package csel.view;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import csel.model.*;

/*
 * Provides the Panel interface which allows for a customizable refresh
 * rate for controlling the rendering speed/framerate.
 * @author Ryan
 */
public class AreaViewPanel extends JPanel implements Runnable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int pWidth = 520;//set Panel width
	private int pHeight = 520;//set Panel height
	private final Thread animator; //animation thread

	public int desiredFrameRate = 20; //set fps
	
	private long period = (long)(1000.0/desiredFrameRate) * 1000000L; //periods between drawings

	private Model model;
	private VisibleTilesGetter vtg = new VisibleTilesGetter();
	private TileRenderer tr;
	private Tile t[][];
	private int count = 0;
	private BufferedImage gameover;
	private Color c;
	private Font f;

	/**
	 * constructor
	 * @param Model m
	 */
	public AreaViewPanel(Model m)
	{
		model = m;
		tr = new TileRenderer(m.getMap());
		vtg = new VisibleTilesGetter();
		setBackground(Color.white);
		setPreferredSize( new Dimension(pWidth, pHeight));
		c = Color.white;
		f = new Font("sansserif", Font.BOLD, 30);
		animator = new Thread(this);
		
		try
		{
			gameover =ImageIO.read(new File("images/gameover.gif"));
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * initialize and start thread
	 */
	public void startLoop()
	{
		animator.start();
		System.out.println("[VIEW] Starting animator thread");
	}
	
	/**
	 * Stops the game
	 */
	public void stopRenderer()
	{
		animator.interrupt();
	}
	
	/**
	 * Animator thread instantiation.
	 * Frames of animation are drawn in while loop
	 */
	public void run()
	{
		synchronized (this)
		{
			System.out.println("[VIEW] Starting render loop");
			while (!Thread.interrupted())
			{
				repaint();
				try
				{
					this.wait(period / 1000000L);
				} catch (InterruptedException ex)
				{
					Thread.currentThread().interrupt();
				}

			}
		}
		System.out.println("[VIEW] AreaViewPanel thread terminated!");
	}
	
	/**
	 * All drawing rendered in this method
	 */
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		
		synchronized (model)
		{
			if(model.getPlayerEntity().getLives() != 0) // player is not dead
			{
				t = vtg.getVisibleTiles(model.getPlayerEntity().getLocation());
				tr.renderTiles(g, t);
				
				//this.drawHello(g);
				
			}
			else // player is dead
			{
				g.drawImage(gameover, 0, 0, 550, 550, 0, 0, 550, 550, null);
				
			}

		}
		//g.drawString(""+count++, 50, 50);
	}
	
	public String toString()
	{
		return this.getClass().getSimpleName();
	}
	
	private void drawHello(Graphics g){
		if(count % 4 == 0 && count < 36){
			c = c.darker();
		}
		else if(count % 4 == 0 && count < 80){
			c = c.brighter();
		}
		if(count < 100){
			count++;
			g.setFont(f);
			g.setColor(c);
			g.drawString("Welcome to the lab-rynth..", 50, 50);
		}
	}
    
   
}
